using System;
using System.Globalization;

namespace Be.Timvw.Framework.Domain.Ranges
{
    /// <summary>
    /// Represents a range of elements.
    /// </summary>
    public class Range<T> : IRange<T> where T : IComparable<T>
    {
        private const string InvalidRangeExceptionMessage = "Invalid Range";

        private readonly T begin;
        private readonly T end;

        /// <summary>
        /// Initializes a new instance of the <see cref="Range&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="begin">The begin.</param>
        /// <param name="end">The end.</param>
        public Range(T begin, T end)
        {
            if(begin.CompareTo(end) > 0)
            {
                throw new ArgumentOutOfRangeException(InvalidRangeExceptionMessage);
            }

            this.begin = begin;
            this.end = end;
        }

        /// <summary>
        /// Gets the Begin.
        /// </summary>
        /// <value></value>
        public T Begin
        {
            get { return this.begin; }
        }

        /// <summary>
        /// Gets the End.
        /// </summary>
        /// <value></value>
        public T End
        {
            get { return this.end; }
        }

        /// <summary>
        /// Tests if element is included.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>true if element is included.</returns>
        public bool Includes(T element)
        {
            if(this.begin.CompareTo(element) > 0)
            {
                return false;
            }

            if(this.end.CompareTo(element) < 0)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Tests if the range is included.
        /// </summary>
        /// <param name="range"></param>
        /// <returns>true if range is included.</returns>
        public bool Includes(IRange<T> range)
        {
            return this.Includes(range.Begin) && this.Includes(range.End);
        }

        /// <summary>
        /// Tests is this range overlaps the given range.
        /// </summary>
        /// <param name="range"></param>
        /// <returns>
        /// true if this range overlaps the given range.
        /// </returns>
        public bool Overlaps(IRange<T> range)
        {
            return range.Includes(this.begin) || range.Includes(this.end) || this.Includes(range);
        }

        /// <summary>
        /// Compares the current object with another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings:
        /// Value
        /// Meaning
        /// Less than zero
        /// This object is less than the <paramref name="other"/> parameter.
        /// Zero
        /// This object is equal to <paramref name="other"/>.
        /// Greater than zero
        /// This object is greater than <paramref name="other"/>.
        /// </returns>
        public int CompareTo(IRange<T> other)
        {
            return InternalCompare(this, other as Range<T>);
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "{0} - {1}", this.begin, this.end);
        }

        /// <summary>
        /// Determines how two ranges compare to each other.
        /// </summary>
        /// <param name="range1">The range1.</param>
        /// <param name="range2">The range2.</param>
        /// <returns></returns>
        private static int InternalCompare(Range<T> range1, Range<T> range2)
        {
            if(ReferenceEquals(range1, range2))
            {
                return 0;
            }
            if(range1 == null)
            {
                return -1;
            }
            if(range2 == null)
            {
                return 1;
            }

            int compare;

            compare = range1.begin.CompareTo(range2.begin);
            if(compare != 0)
            {
                return compare;
            }

            compare = range1.end.CompareTo(range2.end);
            if(compare != 0)
            {
                return compare;
            }

            return compare;
        }

        /// <summary>
        /// Implements the operator &lt;.
        /// </summary>
        /// <param name="range1">The range1.</param>
        /// <param name="range2">The range2.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator <(Range<T> range1, Range<T> range2)
        {
            return InternalCompare(range1, range2) < 0;
        }

        /// <summary>
        /// Implements the operator &gt;.
        /// </summary>
        /// <param name="range1">The range1.</param>
        /// <param name="range2">The range2.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator >(Range<T> range1, Range<T> range2)
        {
            return InternalCompare(range1, range2) > 0;
        }
    }
}